using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using FrameWork.Inventory;

namespace FrameWork.Kit
{
    public class PlayerItemView : MonoBehaviour, IDropHandler, IKitView
    {
        [SerializeField] private string currentItemName;
        [SerializeField] private Image itemImage;
        [SerializeField] private PlayerKit playerKit;

        private void Start()
        {
            //
            EventSystem.Instance.AddListener<string>(EventName.EVENT_INVENTORY_MODEL_ITEMCLEAR, UpdateView);
        }
        
        private void UpdateView(string itemName)
        {
            if (currentItemName == itemName)
            {
                currentItemName = null;
                itemImage.sprite = null;
                itemImage.enabled = false;
                
                //
                Player.Instance.SetCurrentKit(playerKit, null);
            }
        }

        public void OnDrop(PointerEventData eventData)
        {
            // print("拖拽到了目标位置" + "当前位置" + GetComponent<RectTransform>().anchoredPosition);
            // eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
            currentItemName = eventData.pointerDrag.GetComponent<Slot>().GetItem.item.ItemName;
            // 设置图标
            itemImage.gameObject.SetActive(true);
            itemImage.sprite = eventData.pointerDrag.GetComponent<Slot>().GetItem.item.ItemIcon;
        }

        public void SetTool()
        {
            Player.Instance.SetCurrentKit(playerKit, currentItemName);
        }
    }
}
